package com.atguigu.auth.controller;

import com.atguigu.auth.service.SysRoleService;
import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysRole;
import com.atguigu.vo.system.AssginRoleVo;
import com.atguigu.vo.system.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ClassName: SysRoleController
 * Package: com.atguigu.auth.controller
 * Description:
 *
 * @Author: lmf
 * @Create: 2024/1/16 13:09
 * @Version: 1.0
 */
@Api(tags = "角色管理")
@RestController // 在sprint boot中注册 并交由其管理  并返回json格式数据
@RequestMapping("admin/system/sysRole")
public class SysRoleController {
    // 注入service
    private final SysRoleService sysRoleService;

    public SysRoleController(SysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }

    /**
     * 查询所有角色 和 当前用户所属角色
     * @return SysRole
     */
    @ApiOperation("获取用户角色")
    @GetMapping("toAssign/{userid}")
    public Result<Object> toAssign(@PathVariable Long userid) {
        Map<String, Object> map = sysRoleService.findRoleDateByUserId(userid);
        return Result.ok(map);
    }
    /**
     * 为用户分配角色
     */
    @ApiOperation("为用户分配角色")
    @PostMapping
    public Result<Object> doAssign(@RequestBody AssginRoleVo assginRoleVo){
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }



    /**
     * 查询所有角色
     *
     * @return SysRole
     */
    @GetMapping("findAll")
    public Result<Object> findAll() {
        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')") //查询是有此权限才可以查询
    @GetMapping("{page}/{limit}")
    public Result<Object> pageQuery(@PathVariable long page, @PathVariable long limit, SysRoleQueryVo sysRoleQueryVo) {

        // 使用分页
        Page<SysRole> sysRolePage = new Page<>(page, limit);
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if (!StringUtils.isEmpty(roleName)) {
            queryWrapper.like(SysRole::getRoleName, roleName);
        }
        Page<SysRole> page1 = sysRoleService.page(sysRolePage, queryWrapper);
        return Result.ok(page1);

    }

    /**
     * 新增角色
     */
    @PostMapping("save")
    public Result<Object> save(@RequestBody SysRole role) {
        // 调用service的方法
        boolean is_success = sysRoleService.save(role);
        return is_success ? Result.ok() : Result.fail();
    }

    /**
     * 根据id查询
     */
    @GetMapping("findById")
    public Result<Object> findById(@RequestParam("id") Long id) {
        SysRole role = sysRoleService.getById(id);
        return Result.ok(role);
    }

    /**
     * 修改
     */
    @PostMapping("update")
    public Result<Object> update(@RequestBody SysRole role) {
        System.out.println(role);
        boolean is_success = sysRoleService.updateById(role);
        System.out.println(is_success);
        return is_success ? Result.ok() : Result.fail();
    }

    @DeleteMapping("remove/{id}")
    public Result<Object> remove(@PathVariable Long id) {
        System.out.println(id);
        boolean is_success = sysRoleService.removeById(id);
        return is_success ? Result.ok() : Result.fail();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("batchRemove")
    public Result<Object> batchRemove(@RequestBody List<Long> idList) {
        boolean is_success = sysRoleService.removeByIds(idList);
        return is_success ? Result.ok() : Result.fail();
    }


}
